package com.byb.server.service.impl;

import com.byb.server.service.TypeOfCaseService;
import com.byb.server.domain.TypeOfCase;
import com.byb.server.repository.TypeOfCaseRepository;
import com.byb.server.repository.search.TypeOfCaseSearchRepository;
import com.byb.server.service.dto.TypeOfCaseDTO;
import com.byb.server.service.mapper.TypeOfCaseMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing TypeOfCase.
 */
@Service
@Transactional
public class TypeOfCaseServiceImpl implements TypeOfCaseService{

    private final Logger log = LoggerFactory.getLogger(TypeOfCaseServiceImpl.class);

    private final TypeOfCaseRepository typeOfCaseRepository;

    private final TypeOfCaseMapper typeOfCaseMapper;

    private final TypeOfCaseSearchRepository typeOfCaseSearchRepository;
    public TypeOfCaseServiceImpl(TypeOfCaseRepository typeOfCaseRepository, TypeOfCaseMapper typeOfCaseMapper, TypeOfCaseSearchRepository typeOfCaseSearchRepository) {
        this.typeOfCaseRepository = typeOfCaseRepository;
        this.typeOfCaseMapper = typeOfCaseMapper;
        this.typeOfCaseSearchRepository = typeOfCaseSearchRepository;
    }

    /**
     * Save a typeOfCase.
     *
     * @param typeOfCaseDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TypeOfCaseDTO save(TypeOfCaseDTO typeOfCaseDTO) {
        log.debug("Request to save TypeOfCase : {}", typeOfCaseDTO);
        TypeOfCase typeOfCase = typeOfCaseMapper.toEntity(typeOfCaseDTO);
        typeOfCase = typeOfCaseRepository.save(typeOfCase);
        TypeOfCaseDTO result = typeOfCaseMapper.toDto(typeOfCase);
        //typeOfCaseSearchRepository.save(typeOfCase);
        return result;
    }

    /**
     *  Get all the typeOfCases.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TypeOfCaseDTO> findAll(Pageable pageable) {
        log.debug("Request to get all TypeOfCases");
        return typeOfCaseRepository.findAll(pageable)
            .map(typeOfCaseMapper::toDto);
    }

    /**
     *  Get one typeOfCase by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public TypeOfCaseDTO findOne(Long id) {
        log.debug("Request to get TypeOfCase : {}", id);
        TypeOfCase typeOfCase = typeOfCaseRepository.findOne(id);
        return typeOfCaseMapper.toDto(typeOfCase);
    }

    /**
     *  Delete the  typeOfCase by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TypeOfCase : {}", id);
        typeOfCaseRepository.delete(id);
        //typeOfCaseSearchRepository.delete(id);
    }

    /**
     * Search for the typeOfCase corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TypeOfCaseDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TypeOfCases for query {}", query);
        Page<TypeOfCase> result = typeOfCaseSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(typeOfCaseMapper::toDto);
    }
}
